package hk.hku.hooku.servlet;

import hk.hku.hooku.db.UserDB;
import hk.hku.hooku.pojo.User;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Servlet implementation class RegisterServlet
 */
public class RegisterServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public RegisterServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
//		System.out.println("In RegisterServlet.doPost");
		response.setContentType("text/html;charset=gb2312");
		request.setCharacterEncoding("gb2312");
		String username = request.getParameter("username");
		String psw = request.getParameter("password");
		String nickname = request.getParameter("nickname");
		HttpSession session = request.getSession();
		String result = "";
		PrintWriter writer=response.getWriter();
		if ((username == "") || (username == null) || (username.length() > 40)) {
			try {
				result = "Please input your Email (within 40 chars)";
				session.setAttribute("message", result);
				writer.write(result);
			} catch (Exception e) {
				e.printStackTrace();
			}
			return;
		}
		if ((psw == "") || (psw == null) || (psw.length() > 20)) {
			try {
				result = "Please input password";
				session.setAttribute("message", result);
				writer.write(result);
			} catch (Exception e) {
				e.printStackTrace();
			}
			return;
		}
		User user = UserDB.getFullUserByGmail(username);
		if (user != null) {
			try {
				result = "This user exists!";
				session.setAttribute("message", result);
				writer.write(result);
			} catch (Exception e) {
				e.printStackTrace();
			}
			return;
		} else {
			boolean success = UserDB.addUser(username, psw, nickname);
			if (success) {
				try {
					result = "Register success! Please login.";
					session.setAttribute("message", result);
					session.setAttribute("loginUser", username);
					writer.write("success");
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
			else {
				try {
					result = "Register failed! Please try again later.";
					session.setAttribute("message", result);
					writer.write(result);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
	}

}
